package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Account;
import org.csu.mypetstore.domin.Banner;
import org.csu.mypetstore.domin.SignOn;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.service.AccountService;

import org.csu.mypetstore.service.SignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("AccountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private SignOnService signOnService;

    @Autowired
    private BannerServiceImpl bannerService;
    @Override
    public CommonResponse<Account> getAccountByUsername(String username) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",username);
        Account account=accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("通过username无法找到该用户");
        }
        return CommonResponse.createForSuccess("查找成功",account);
    }

    @Override
    public CommonResponse<Account> getAccountByUsernameAndPassword(String username, String password) {
        SignOn signOn=new SignOn(username,password);
        CommonResponse commonResponse=signOnService.getSignOnByUsernameAndPassword(signOn);
        if(commonResponse.getStatus()==0){
            return CommonResponse.createForError("通过username和password无法找到该用户");
        }
        Account account=getAccountByUsername(username).getData();
        account.setPassword(password);
        return CommonResponse.createForSuccess("登陆成功",account);
    }

    @Override
    public CommonResponse<Account> insertAccount(Account account) {
        CommonResponse commonResponse=getAccountByUsername(account.getUserid());
        if(commonResponse.getStatus()==1){
            return CommonResponse.createForError("用户名已存在");
        }
        SignOn signOn=new SignOn(account.getUserid(),account.getPassword());
        CommonResponse commonResponse1=signOnService.insertSignOn(signOn);
        int x=accountMapper.insert(account);
        if(x==0|| commonResponse1.getStatus()==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        Banner banner = new Banner(account.getUserid());
        bannerService.addBanner(banner);
        return CommonResponse.createForSuccess("新增用户成功",account);
    }

    @Override
    public CommonResponse<Account> getAccountByEmail(String email) {
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        Account account=accountMapper.selectOne(queryWrapper);
        if(account==null){
            return CommonResponse.createForError("通过email无法找到该用户");
        }
        return CommonResponse.createForSuccess("通过email查找成功",account);
    }

    @Override
    public CommonResponse<Account> updateAccount(Account account) {
        UpdateWrapper<Account> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("userid",account.getUserid());
        int x=accountMapper.update(account,updateWrapper);
        SignOn signOn=new SignOn(account.getUserid(),account.getPassword());
        CommonResponse commonResponse=signOnService.updateSignOn(signOn);
        if(x==0|| commonResponse.getStatus()==0){
            return CommonResponse.createForError("服务器内部异常");
        }
        return CommonResponse.createForSuccess("更新用户成功",account);
    }

    @Override
    public CommonResponse<Account> getAccountByPhone(String phone) {
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("phone",phone));
        if(account == null){
            return CommonResponse.createForError("手机号不存在!");
        }
        return CommonResponse.createForSuccess("手机号存在",account);
    }
}
